package GUI;

import java.awt.Color;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import Server.EchoServer;
import Server.IP_Identify;

import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JLabel;
import java.awt.Font;

/**
 * this class impliments the server gui 
 
 */

public class ServerGui extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private EchoServer server;
	private String ipn;
    public ServerGui() {
    	IP_Identify ip = new IP_Identify();
    	ipn=ip.IPIdentify();
        initComponents();
    }

   /**
    * initilize the server window 
    */
    private void initComponents() {

        jButtonStart = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jLabelHeadline = new javax.swing.JLabel();
        jLabelState = new javax.swing.JLabel();   

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonStart.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
        jButtonStart.setText("Start Server");
        jButtonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	int x=0;
				while(server.y==1);
                jButtonStartActionPerformed(evt);
            }
        });

        jButtonStop.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
        jButtonStop.setText("Stop Server");
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });

        jLabelHeadline.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
        jLabelHeadline.setText("VCP Server Administration");

        jLabelState.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
        jLabelState.setForeground(new java.awt.Color(255, 51, 51));
        jLabelState.setText("Off Line");
        JLabel lblSereverIp = new JLabel("Server IP :");
        
        ipnumber = new JLabel("");
        ipnumber.setForeground(Color.BLUE);
        ipnumber.setFont(new Font("Tahoma", Font.BOLD, 14));
        ServerGui.ipnumber.setText(ipn);
        
        JLabel lblPort = new JLabel("Database port");
        
        portTextField = new JTextField();
        portTextField.setColumns(10);
        
        portTextField.setText("5555");
        
        
        JLabel lblSchemaName = new JLabel("Schema name");
        
        nameTextField = new JTextField();
        nameTextField.setColumns(10);
nameTextField.setText("test");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap(19, Short.MAX_VALUE)
        			.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        				.addGroup(layout.createSequentialGroup()
        					
        					.addGap(143))
        				.addGroup(layout.createSequentialGroup()
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        							.addGroup(layout.createSequentialGroup()
        								.addComponent(jLabelState)
        								.addGap(145))
        							.addGroup(layout.createSequentialGroup()
        								.addGroup(layout.createParallelGroup(Alignment.LEADING)
        									.addGroup(layout.createSequentialGroup()
        										.addComponent(lblPort)
        										.addPreferredGap(ComponentPlacement.RELATED)
        										.addComponent(portTextField, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
        										.addGap(18)
        										.addComponent(lblSchemaName)
        										.addPreferredGap(ComponentPlacement.RELATED)
        										.addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        									.addComponent(jLabelHeadline))
        								.addGap(40)))
        						.addGroup(layout.createSequentialGroup()
        							.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        								.addGroup(layout.createSequentialGroup()
        									.addComponent(lblSereverIp)
        									.addPreferredGap(ComponentPlacement.RELATED)
        									.addComponent(ipnumber, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
        								.addComponent(jButtonStart, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))
        							.addGap(18)
        							.addComponent(jButtonStop, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
        							.addPreferredGap(ComponentPlacement.RELATED)))
        					.addGap(10))))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(jLabelHeadline)
        			.addGap(16)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        			
        				.addComponent(lblPort)
        				.addComponent(portTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblSchemaName)
        				.addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(32)
        			.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        				.addComponent(jButtonStop, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addComponent(jButtonStart, GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addComponent(ipnumber)
        					.addGap(11)
        					.addComponent(jLabelState))
        				.addComponent(lblSereverIp))
        			.addContainerGap(22, Short.MAX_VALUE))
        );
        layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {jButtonStart, jButtonStop});
        getContentPane().setLayout(layout);

        pack();
    }// </editor-fold>

    private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {
    	int port = 5555;    	
    	if(portTextField.getText().equals(""))
    		port=5555;
    	else
    		port=Integer.parseInt(portTextField.getText());
    	server = new EchoServer(port);
		try { 
			server.listen(); // Start listening for connections
			jLabelState.setForeground(new Color(0,0,255));
			 jLabelState.setText("On Line");
			 
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(this, "ERROR - Could not listen for clients!", "Information", JOptionPane.INFORMATION_MESSAGE);		
		}		
    }

    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {
    	try {
			server.close();
			jLabelState.setForeground(new Color(255,0,0));
			jLabelState.setText("Off Line");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    	    	
    }

    /**
     * run the server 
     * @param args the command line arguments
     */
    public static void main(String args[]) {      
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ServerGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButtonStart;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JLabel jLabelHeadline;
    private javax.swing.JLabel jLabelState;
    private static  JLabel ipnumber;
    private JTextField portTextField;
    private JTextField nameTextField;
}
